from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession
from app.db.session import get_session
from app.services.model_router import call_model
from app.schemas.chat import ChatRequest, ChatResponse

router = APIRouter(tags=["chat"])

@router.post("/", response_model=ChatResponse)
async def chat(request: ChatRequest, db: AsyncSession = Depends(get_session)):
    """
    model: 'openai', 'wenxin', 'qianfan'
    message: 用户输入
    session_id: 对话会话 id（可选）
    """
    # 将 session_id 作为 conversation_id 传给 Qianfan
    reply = await call_model(request.model, request.message)

    # TODO: 将 message + reply + session_id 存入 PostgreSQL
    # await save_message_to_db(db, request.session_id, request.message, reply)

    return ChatResponse(reply=reply)
